package com.study.stream;

import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

public class DemoStream {
    public static List<CmdTasker> taskList = new ArrayList<>();

    static {
        System.out.println("静态方法块执行");
        List<String> strings = Arrays.asList("A", "B", "C");
        for (String sId : strings) {
            for (int i = 0; i < 10; i++) {
                CmdTasker tasker = new CmdTasker();
                tasker.setId(String.format("%s_%s", sId, i));
                tasker.setCommand(String.format("%s_%s", sId, i));
                tasker.setProcess(false);
                tasker.setThread(false);
                if (i % 4 == 0) {
                    tasker.setProcess(true);
                    tasker.setThread(true);
                }
                taskList.add(tasker);
            }
        }
    }

    public static void main(String[] args) {
        for (CmdTasker cmdTasker : taskList) {

            System.out.println(cmdTasker);
        }
        System.out.println("---------------------");
        groupById();
        System.out.println(StringUtils.center("提取对象某个字段", 25, "*"));
        List<String> collect = taskList.stream().map(CmdTasker::getId).collect(Collectors.toList());
        System.out.println("提取结果");
        System.out.println(collect);
    }


    /**
     * java sstream 分组
     */
    static void groupById() {
        Map<String, List<CmdTasker>> collect =
                taskList.stream().collect(Collectors.groupingBy(CmdTasker::type));
        for (Map.Entry<String, List<CmdTasker>> stuMap : collect.entrySet()) {
            String classId = stuMap.getKey();
            List<CmdTasker> studentList = stuMap.getValue();
            System.out.println("ID:" + classId + ",values:" + studentList.toString());
        }
        Set<String> keySet = collect.keySet();
        for (String key : keySet) {
            System.out.println("keys :" + key);
        }
        // 分组的lambda写法
        taskList.stream().collect(Collectors.groupingBy(cmdTasker -> {
            return cmdTasker.type();
        }));
    }
    //差集|https://www.cnblogs.com/changfanchangle/p/8966860.html
}
